package com.gtw.modules.api.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.common.utils.WeiXinMsgUtil;
import com.gtw.common.validator.Assert;
import com.gtw.dto.TbUserDto;
import com.gtw.modules.api.service.TbTokenService;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbToken;
import com.gtw.modules.entity.TbUser;
import com.gtw.utils.EntityDtoConverter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 普通户用
 *
 * @author wuye
 * @date 2018/1/17
 */
@RestController
@RequestMapping("/api/user")
public class ApiTbUserController {

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private TbTokenService tokenService;


    /**
     * 查询列表
     *
     * @param params
     * @returnapi
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TbUser> pageUtil = new Page<TbUser>(query.getPage(), query.getLimit());
        Page<TbUser> page = tbUserService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }

    /**
     * 添加普通用户
     *
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody TbUser tbUser) {
        tbUserService.insert(tbUser);
        return R.ok();
    }

    /**
     * 修改普通用户
     *
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbUser tbUser) {
        tbUserService.update(tbUser);
        return R.ok();
    }


    /**
     * 判断上级用户身份
     *
     * @return
     */
    @RequestMapping("/judgment")
    public R judgment(@LoginUserApi TbUser tbUser, Integer parentid) {
        TbUser parent = tbUserService.foundById(Long.valueOf(parentid));
        if (parent == null) {
            return R.error().put("msg", "该邀请码用户不存在");
        }
        if (parent.getType() == 0) {
            return R.error().put("msg", "该用户不是会员或代理!没有权限邀请用户");
        }
        if (tbUser.getParentid() == 1) {
            tbUser.setParentid(parentid);
            bindParend(tbUser,parent);
            return R.ok();
        } else {
            return R.error("已经修改过了");
        }
    }
    /**
     * 绑定上下级关系,
     * @param tbUser
     * @param parent
     */
    private void bindParend(TbUser tbUser, TbUser parent) {
        WeiXinMsgUtil.tuanduifensi(parent.getWxopenid(),String.valueOf(tbUser.getUserId()),tbUser.getUsername());
        if (tbUserService.queryById(parent.getParentid()) != null) {
            TbUser user2 = tbUserService.queryById(parent.getParentid());
            WeiXinMsgUtil.tuanduifensi(user2.getWxopenid(),String.valueOf(tbUser.getUserId()),tbUser.getUsername());
            tbUser.setParentid2(user2.getUserId());
            if (tbUserService.queryById(user2.getParentid()) != null) {
                TbUser user3 = tbUserService.queryById(user2.getParentid());
                tbUser.setParentid3(user3.getUserId());
                if (tbUserService.queryById(user3.getParentid()) != null) {
                    TbUser user4 = tbUserService.queryById(user3.getParentid());
                    tbUser.setParentid4(user4.getUserId());
                }
            }
        }
        tbUserService.update(tbUser);
    }

    /**
     * 删除普通用户(软删除)
     *
     * @param id
     * @return
     */
    public R delete(@RequestParam("id") int id) {
        tbUserService.deleteById(id);
        return R.ok();
    }

    /**
     * 通过token获取个人资料
     *
     * @return
     */
    @GetMapping("/info")
    public R info(@LoginUserApi TbUser tbUser) throws InvocationTargetException, IllegalAccessException {
        tbUser = tbUserService.queryByUid(tbUser.getUuid());

        TbToken tbToken = tokenService.queryByUserId((long) tbUser.getUserId());
        tbUser = tbUserService.foundById(tbToken.getUserId());

        TbUserDto tbUserDto = new TbUserDto();
        EntityDtoConverter edc = new EntityDtoConverter();
        tbUserDto = (TbUserDto) edc.entityConvertDto(tbUser, tbUserDto);
        tbUserDto.setTbToken(tbToken);
        return R.ok().put("data", tbUserDto);
    }

    /**
     * 通过uid获取个人资料
     *
     * @return
     */
    @RequestMapping("/infouid")
    public R infoByuid(Integer uid) {
        TbUser tbUser = tbUserService.queryById(uid);
        return R.ok().put("data", tbUser);
    }

    /**
     * 通过phone获取个人资料
     *
     * @return
     */
    @RequestMapping("/phone")
    public R infoByphone(String phone) {
        TbUser tbUser = tbUserService.queryByMobile(phone);
        return R.ok().put("data", tbUser);
    }


}
